package com.ruoyi.system.service.impl;

import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IIdleAffiliateCpaIncomeDetailService;
import com.ruoyi.system.service.IIdleAffiliateCpsIncomeDetailService;
import com.ruoyi.system.service.IIdleAffiliateIncomeStatisticsService;

/**
 * 闲鱼联盟收入统计Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class IdleAffiliateIncomeStatisticsServiceImpl implements IIdleAffiliateIncomeStatisticsService 
{
    @Autowired
    private IIdleAffiliateCpaIncomeDetailService cpaIncomeDetailService;

    @Autowired
    private IIdleAffiliateCpsIncomeDetailService cpsIncomeDetailService;

    /**
     * 获取CPA和CPS收入汇总统计信息
     * 
     * @return 统计信息，包含CPA和CPS的总金额、已结算金额、未结算金额和差值
     */
    @Override
    public Map<String, Object> getCombinedIncomeStatistics()
    {
        Map<String, Object> result = new HashMap<>();
        
        // 获取CPA统计信息
        Map<String, Object> cpaStats = cpaIncomeDetailService.getCpaIncomeStatistics();
        // 获取CPS统计信息
        Map<String, Object> cpsStats = cpsIncomeDetailService.getCpsIncomeStatistics();
        
        // 计算总金额
        BigDecimal totalAmount = ((BigDecimal)cpaStats.get("totalAmount")).add((BigDecimal)cpsStats.get("totalAmount"));
        // 计算已结算金额
        BigDecimal settledAmount = (BigDecimal)cpsStats.get("settledAmount");
        // 计算未结算金额
        BigDecimal unsettledAmount = ((BigDecimal)cpaStats.get("totalAmount")).add((BigDecimal)cpsStats.get("unsettledAmount"));
        
        // 设置CPA统计信息
        result.put("cpa", cpaStats);
        // 设置CPS统计信息
        result.put("cps", cpsStats);
        // 设置汇总信息
        result.put("totalAmount", totalAmount);
        result.put("settledAmount", settledAmount);
        result.put("unsettledAmount", unsettledAmount);
        result.put("difference", unsettledAmount);
        
        return result;
    }
} 